﻿using Microsoft.EntityFrameworkCore;
using Zhaoxi.ReadingPlatform.Models;
using Zhaoxi.ReadingPlatform.Tools;

namespace Zhaoxi.ReadingPlatform.DataAccess
{
    public class ReadingDbContext : DbContext
    {
        public ReadingDbContext() { }
        public ReadingDbContext(DbContextOptions<ReadingDbContext> options) : base(options) { }

        public virtual DbSet<BookEntity> BookEntities { get; set; }
        public virtual DbSet<BookChapterInfo> BookChapterInfos { get; set; }

        /// <summary>
        /// 配置参数，例如：数据库连接字符串
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (optionsBuilder.IsConfigured)
            {
                return;
            }

            optionsBuilder.UseSqlServer(GlobalVariables.SQLCONNECTIONURL);
        }

        /// <summary>
        /// 配置数据库表的映射关系
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<BookEntity>(entity =>
            {
                entity.ToTable(nameof(BookEntity));
            });

            modelBuilder.Entity<BookChapterInfo>(entity =>
            {
                entity.ToTable(nameof(BookChapterInfo));
            });
        }
    }
}
